package org.example.console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Scanner;

public class ReadInputAndPrintConsole {


    /**
     * 读取键盘的输入，并且打印输入的字符到console
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //1.读取键盘的输入
        readSingleChar();
    }


    public static void readSingleChar() throws Exception {
        Scanner scanner = new Scanner(System.in);
        PrintWriter printWriter = new PrintWriter(System.out, true);
        System.out.println("请输入：");
        while (scanner.hasNext()) {
            String nextStr = scanner.next();
            StringReader stringReader = new StringReader(nextStr);
            char[] buf = new char[10];
            int hasRead = 0;
            while ((hasRead = stringReader.read(buf)) != -1) {
                printWriter.println(buf);
            }
        }

    }


    public static void testInputAndPrint() {
        //1.7 try with resource写法
        try (PrintWriter out = new PrintWriter(System.out, true);
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.println("请输入:");
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.equals("exit")) {
                    System.exit(1);
                }
                out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
